<?php
session_cache_expire(999999999);
if(!isset($_SESSION)) 
{ 
	session_start(); 
}
	
	$lang = $_SESSION['lang'];
	if (empty($lang) || $lang == "EN") {
		include "../value/value_en.php";
	} else if($lang == "VN") {
		include "../value/value_vn.php";
	}
	else {
		include "../value/value_kr.php";
	}
include '../operations/connection.php';
	$id=$_REQUEST['id'];
	$id_employee=$_SESSION['IDEmployee'];
	$sql="SELECT nf.nfid,nf.pr_idproject,nf.nf_order,nf.nf_title,nf.nf_content,nf.nf_createdate,nf.nf_employee,nf.nf_employee_name, pr.IDView from pr_newfunction nf,pr_project pr WHERE pr.IDProject=nf.pr_idproject and nf.pr_idproject='".$id."' ORDER BY nf.nf_createdate desc";
	$queryListErrorByIDPr = mysql_query($sql);
	
	
	$querydetail=mysql_query("SELECT p.NameProject, p.Description,DATE_FORMAT( p.DateStart, '%Y-%m-%d') as DateStart, p.DateComplete, p.IDManager,p.IDView, p.Status
	FROM pr_project p,pr_composition c, employee e WHERE ((c.IDEmployee = '$id_employee'  AND e.IDEmployee = c.IDEmployee )
	 or (p.IDManager = '$id_employee'  AND e.IDEmployee = p.IDManager )) AND p.IDProject = c.IDProject AND  p.IDProject ='$id'
	 GROUP BY p.IDProject ORDER BY p.NameProject ASC 
	");


	
	//include '../Classes/PHPExcel.php';
	/** Include path **/
	set_include_path(get_include_path() . PATH_SEPARATOR . 'ClassesExcel/');
	
	/** PHPExcel */
	require_once 'PHPExcel.php';
	
	/** PHPExcel_RichText */
	require_once 'PHPExcel/RichText.php';
	
	
	$filename ="export_project.xls";
	$objPHPExcel = new PHPExcel();

	
	$styleArray = array(
		'font' => array(
			'bold' => true,
			'size' => 11,
		),
		'alignment' => array(
			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
			'wrap' => true,
		),
		'borders' => array(
			'top' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'bottom' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'left' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'right' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
		),
		'fill' => array(
			'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
			'rotation' => 90,
			'startcolor' => array(
				'argb' => 'FFA0A0A0',
			),
			'endcolor' => array(
				'argb' => 'FFFFFFFF',
			),
		),
	);
	
	$styleArray2 = array(
			'alignment' => array(
			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
			'wrap' => true,
		),
		'borders' => array(
			'top' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'bottom' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'left' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'right' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
		),
		
	);
	$styleArray3 = array(
			'alignment' => array(
			'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
			'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
			'wrap' => true,
		),
		'borders' => array(
			'top' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'bottom' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'left' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
			'right' => array(
				'style' => PHPExcel_Style_Border::BORDER_THIN,
			),
		),
		
	);
	
	
	
	$objPHPExcel->setActiveSheetIndex(0);
	while ($row=mysql_fetch_array($querydetail)){ 
		$idmanager = $row[IDManager];

		$queryManger =mysql_query("SELECT emp.FullName FROM employee emp WHERE emp.IDEmployee = '$idmanager'" );
		
		$objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setName('Times New Roman');
		$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(25);
		$objPHPExcel->getActiveSheet()->mergeCells('A1:E1');
		$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);
		$objPHPExcel->getActiveSheet()->setCellValue('A1',$l_project." : ".$row['NameProject']);
		
		$filename ="export_project_feature_".$row['NameProject'].".xls";
		
		$objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(25);
		$objPHPExcel->getActiveSheet()->mergeCells('A2:B2');
		$objPHPExcel->getActiveSheet()->getStyle('A2')->applyFromArray($styleArray3);
		$objPHPExcel->getActiveSheet()->getStyle('B2')->applyFromArray($styleArray3);
		$objPHPExcel->getActiveSheet()->setCellValue('A2',$l_start_date." : ".$row['DateStart']);
	
		while ($r = mysql_fetch_array($queryManger)){
			$objPHPExcel->getActiveSheet()->getRowDimension('3')->setRowHeight(25);
			$objPHPExcel->getActiveSheet()->mergeCells('A3:B3');
			$objPHPExcel->getActiveSheet()->getStyle('A3')->applyFromArray($styleArray3);
			$objPHPExcel->getActiveSheet()->getStyle('B3')->applyFromArray($styleArray3);
			$objPHPExcel->getActiveSheet()->setCellValue('A3',$l_manager." : ".$r['FullName']);
		}
		
		$status="";
		if($row['Status']==0){
			$status= $l_status_project_no;
		}else{
			$status= $l_status_project_done;
		}
		$objPHPExcel->getActiveSheet()->getRowDimension('4')->setRowHeight(25);
		$objPHPExcel->getActiveSheet()->mergeCells('A4:B4');
		$objPHPExcel->getActiveSheet()->getStyle('A4')->applyFromArray($styleArray3);
		$objPHPExcel->getActiveSheet()->getStyle('B4')->applyFromArray($styleArray3);
		$objPHPExcel->getActiveSheet()->setCellValue('A4',$l_status." : ".$status);
	
	}
	
	
	$objPHPExcel->getActiveSheet()->freezePane('A6');
	
	$objPHPExcel->getActiveSheet()->getRowDimension('5')->setRowHeight(25);
	$objPHPExcel->getActiveSheet()->getStyle('A5')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
	$objPHPExcel->getActiveSheet()->setCellValue('A5',$l_no);
	
	$objPHPExcel->getActiveSheet()->getStyle('B5')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
	$objPHPExcel->getActiveSheet()->setCellValue('B5', $l_code);
	
	$objPHPExcel->getActiveSheet()->getStyle('C5')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(100);
	$objPHPExcel->getActiveSheet()->setCellValue('C5', $l_title);
	
	
	$objPHPExcel->getActiveSheet()->getStyle('D5')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->setCellValue('D5', $l_create_by);
	
	$objPHPExcel->getActiveSheet()->getStyle('E5')->applyFromArray($styleArray);
	$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
	$objPHPExcel->getActiveSheet()->setCellValue('E5', $l_created_date);
	$key=6;
	$idviewpro=$_SESSION['projectinfo']['idviewpro'];
	if(mysql_num_rows($queryListErrorByIDPr)>0){
			$vitri=1;
			while ($row=mysql_fetch_array($queryListErrorByIDPr)){
				
				$objPHPExcel->getActiveSheet()->getRowDimension($key)->setRowHeight(25);
				$objPHPExcel->getActiveSheet()->getStyle('A'.$key)->applyFromArray($styleArray3);
				$objPHPExcel->getActiveSheet()->setCellValue('A'.$key, $vitri);
				
				$objPHPExcel->getActiveSheet()->getStyle('B'.$key)->applyFromArray($styleArray3);
				$objPHPExcel->getActiveSheet()->setCellValue('B'.$key, ($idviewpro.'-'.$row['nf_order']));

				$s1="select nf_contentcomment,nf_employee_name from pr_newfunction_comment where nfid_newfunction='".$row['nfid']."' order by nf_datecomment desc limit 0,1";
				$rs=mysql_query($s1);
				while($r=mysql_fetch_array($rs)){
					$chuoi=chop(stripslashes($r['nf_contentcomment']));
					if(isset($chuoi[27])){
						$stringcolortest= substr($chuoi,0,27)."...(".$r['nf_employee_name'].")";
					}else{
						$stringcolortest= $chuoi." (".$r['nf_employee_name'].")";
					}
				}
				$objPHPExcel->getActiveSheet()->getStyle('C'.$key)->applyFromArray($styleArray3);
				$objPHPExcel->getActiveSheet()->setCellValue('C'.$key, $row['nf_title']);
				
				
				$objPHPExcel->getActiveSheet()->getStyle('D'.$key)->applyFromArray($styleArray3);
				$objPHPExcel->getActiveSheet()->setCellValue('D'.$key,$row['nf_employee_name']);
				
				$objPHPExcel->getActiveSheet()->getStyle('E'.$key)->applyFromArray($styleArray3);
				$objPHPExcel->getActiveSheet()->setCellValue('E'.$key, $row['nf_createdate']);
				
				$vitri++;
		        $key++;
			}
	}
	include 'PHPExcel/IOFactory.php';
	
	header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
	header("Cache-Control: no-store, no-cache, must-revalidate");
	header("Cache-Control: post-check=0, pre-check=0", false);
	header("Pragma: no-cache");
	header("Content-type: application/vnd.ms-excel");
	header('Content-Disposition: attachment; filename=' . $filename);
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
	$objWriter->save('php://output');

	mysql_close($connection);
exit;

?>

